package com.example.java_chatroom.common;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class common {
    public static WebDriver driver = null;
    public WebDriverWait wait = null;
    public static String detailUrl = "http://127.0.0.1:8080/login.html";

    //获取到url驱动
    public common(String url)
    {
        //调用driver对象
        driver = createDriver();

        driver.get(url);

        wait = new WebDriverWait(driver, Duration.ofSeconds(3));
    }



    public static WebDriver createDriver()
    {
        if(driver == null)
        {
            //下载驱动
            WebDriverManager.chromedriver().setup();
            //添加配置：允许访问所有的连接
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--remote-allow-origins=*");
            //创建驱动对象
            driver = new ChromeDriver(options);

            //隐式等待——只等待页面元素
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        }
        return  driver;
    }

    public static void quit()
    {
        if(driver != null)
            driver.quit();
    }

}